package com.ruoyi.system.sale.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.message.domain.MiniMessage;
import com.ruoyi.system.message.service.IMiniMessageService;
import com.ruoyi.system.sale.domain.Sale;
import com.ruoyi.system.sale.mapper.SaleMapper;
import com.ruoyi.system.sale.service.ISaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 预警信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-16
 */
@Service
public class SaleServiceImpl implements ISaleService
{
    @Autowired
    private SaleMapper saleMapper;

    @Autowired
    private IMiniMessageService iMiniMessageService;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询预警信息
     *
     * @param id 预警信息主键
     * @return 预警信息
     */
    @Override
    public Sale selectSaleById(String id)
    {
        return saleMapper.selectSaleById(id);
    }

    /**
     * 查询预警信息列表
     *
     * @param sale 预警信息
     * @return 预警信息
     */
    @Override
    public List<Sale> selectSaleList(Sale sale)
    {
        return saleMapper.selectSaleList(sale);
    }

    /**
     * 新增预警信息
     *
     * @param sale 预警信息
     * @return 结果
     */
    @Override
    public int insertSale(Sale sale)
    {
        sale.setCreatedAt(new Date());
        sale.setUpdatedAt(new Date());
        sale.setCreatedBy(SecurityUtils.getUserId().toString());
        sale.setUpdatedBy(SecurityUtils.getUserId().toString());
        saleMapper.insertSale(sale);
        // 获取系统内所有用户，每人发送一条信息
        List<SysUser> sysUsers = sysUserMapper.selectUserList(new SysUser());
        sysUsers.forEach(sysUser -> {
            CompletableFuture.runAsync(() -> insertMessage(sale.getText() , sysUser.getUserId() , LocalDateTime.now()));
        });
        return 1;
    }

    /**
     * 修改预警信息
     *
     * @param sale 预警信息
     * @return 结果
     */
    @Override
    public int updateSale(Sale sale)
    {
        return saleMapper.updateSale(sale);
    }

    /**
     * 批量删除预警信息
     *
     * @param ids 需要删除的预警信息主键
     * @return 结果
     */
    @Override
    public int deleteSaleByIds(String[] ids)
    {
        return saleMapper.deleteSaleByIds(ids);
    }

    /**
     * 删除预警信息信息
     *
     * @param id 预警信息主键
     * @return 结果
     */
    @Override
    public int deleteSaleById(String id)
    {
        return saleMapper.deleteSaleById(id);
    }

    private void insertMessage(String content, Long userId, LocalDateTime reminderTime) {
        MiniMessage message = new MiniMessage();
        message.setMessageContent(content);
        message.setMessageTitle("预警消息提醒");
        Instant instant = reminderTime.minusMinutes(0L).atZone(ZoneId.systemDefault()).toInstant();
        message.setRecipient(Long.parseLong(userId.toString()));
        message.setReminderTime(Date.from(instant));
        message.setSendTime(Date.from(instant));
        message.setSender("系统");
        message.setSendMode(2);
        iMiniMessageService.insertData(message);
    }
}
